//
//  PayNoticeContoller.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/26.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
class PayNoticeContoller: BaseController {
    var payWay = ""//支付方式
    var noticeView: PayNoticeView = {
        let view = PayNoticeView(frame: CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW, height: kScreenH - kStatusBarH + kNavigationBarH))
        return view
    }()
    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupNavBar(title: "支付结果", isback: false, appearWay: 1, isright: false)//自定义导航
        if reachability.isReachable {
            loadPayInfo()
            noticeView.moneyText = payMoney
            if "weixinPay" == payWay {
                let weixinImg = UIImage(named: "weixin")
                noticeView.noticeImg = weixinImg
            }
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
extension PayNoticeContoller{
    func loadPayInfo(){
        let url = projectUrl+"/pay/payinfo/\(payNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
        Alamofire.request("\(url)",method:.post, parameters:nil,headers:headers).responseJSON { (response) in
            let jsonString = response.result.value!
            let json = JSON(jsonString)
            print("json\(json)")
            if json["success"] == true {
                if("2" == json["rdata"]["payStatus"]){//支付成功
                    self.noticeView.messText = "支付成功"
                }else{
                    self.noticeView.messText = "支付失败"
                }
                self.view.addSubview(self.noticeView)
            }else{
                self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
            }
        }
    }
    func finishPayFun(){
        payNo = ""
        payMoney = ""
        //跳转到订单列表页面
        let mvc = MainViewController()
        mvc.selectedIndex = 1
        let mainVC = UINavigationController(rootViewController: mvc)
        app.window?.rootViewController = mainVC
    }
}
